using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using DevExpress.LookAndFeel;
using DevExpress.Skins;

namespace Demo
{
    public static class SkinManager
    {
        private static string configFilePath = Path.Combine(Application.StartupPath, "skinconfig.json");
        
        // 动态获取所有可用的皮肤（包括外置皮肤）
        public static string[] GetAvailableSkins()
        {
            // 合并内置皮肤和 Bonus Skins
            var allSkins = new List<string>();
            
            // 添加内置皮肤
            allSkins.AddRange(DefaultBuiltInSkins);
            
            // 添加 Bonus Skins（外置皮肤）
            allSkins.AddRange(BonusSkins);
            
            return allSkins.Distinct().OrderBy(s => s).ToArray();
        }
        
        // Bonus Skins（外置皮肤）列表 - 公开以便外部访问
        public static readonly string[] BonusSkins = new string[]
        {
            "Sharp Plus",
            "Sharp",
            "Money Twins",
            "Lilian",
            "Foggy",
            "Glass Oceans",
            "Stardust",
            "Xmas (Blue)",
            "Xmas (Red)",
            "DevExpress Dark Style",
            "DevExpress Style",
            "Caramel",
            "Coffee",
            "Dark Side",
            "Liquid Sky",
            "Techno",
            "Darkroom",
            "Springtime",
            "The Asphalt World",
            "Pumpkin",
            "Deep Ocean",
            "Ink",
            "Red White",
            "Darkred",
            "Dreamy",
            "Blueprint",
            "Born2Blue",
            "Casual",
            "Orange",
            "Our Skin",
            "Skins or Roses",
            "Black",
            "Blue",
            "Green",
            "Pink",
            "Red",
            "Teal",
            "Violet",
            "Purple",
            "Cyan",
            "Lime",
            "Indigo",
            "Brown",
            "Amethyst",
            "Aqua",
            "Chocolate",
            "Cobalt",
            "Emerald",
            "High Contrast",
            "Magenta",
            "Marine",
            "McSkin",
            "Morning",
            "Silver",
            "Yellow",
            "Zerg"
        };
        
        // 兼容旧代码的属性
        public static string[] AvailableSkins => GetAvailableSkins();
        
        // 公开内置皮肤列表供外部使用
        public static string[] DefaultBuiltInSkins => new string[]
        {
            "Office 2019 Colorful",
            "Office 2019 Dark Gray",
            "Office 2019 White",
            "Office 2016 Colorful",
            "Office 2016 Dark",
            "Office 2013 Light Gray",
            "Office 2013 Dark Gray",
            "Office 2013 White",
            "Visual Studio 2013 Light",
            "Visual Studio 2013 Dark",
            "The Bezier",
            "Seven Classic",
            "Seven",
            "VS2010",
            "Office 2010 Blue",
            "Office 2010 Silver",
            "Office 2010 Black",
            "Metropolis",
            "Metropolis Dark",
            "iMaginary"
        };

        public static void LoadSkin()
        {
            try
            {
                if (File.Exists(configFilePath))
                {
                    var json = File.ReadAllText(configFilePath, Encoding.UTF8);
                    var config = JsonSerializer.Deserialize<SkinConfig>(json);
                    if (config != null && !string.IsNullOrEmpty(config.SkinName))
                    {
                        // 检查皮肤是否存在
                        var availableSkins = GetAvailableSkins();
                        if (availableSkins.Contains(config.SkinName))
                        {
                            UserLookAndFeel.Default.SetSkinStyle(config.SkinName);
                            return;
                        }
                    }
                }
            }
            catch { }
            
            // 默认皮肤 - 使用第一个可用皮肤
            var skins = GetAvailableSkins();
            if (skins.Length > 0)
            {
                // 优先使用现代化的皮肤
                var preferredSkin = skins.FirstOrDefault(s => s.Contains("Office 2019") || s.Contains("Office 2016") || s.Contains("Metropolis"));
                UserLookAndFeel.Default.SetSkinStyle(preferredSkin ?? skins[0]);
            }
            else
            {
                UserLookAndFeel.Default.SetSkinStyle("Office 2019 Colorful");
            }
        }

        public static void SaveSkin(string skinName)
        {
            try
            {
                var config = new SkinConfig { SkinName = skinName };
                var json = JsonSerializer.Serialize(config, new JsonSerializerOptions { WriteIndented = true });
                File.WriteAllText(configFilePath, json, Encoding.UTF8);
                
                // 尝试应用皮肤
                try
                {
                    UserLookAndFeel.Default.SetSkinStyle(skinName);
                }
                catch (Exception ex)
                {
                    // 保存设置，即使当前无法应用（可能在重启后生效）
                    System.Diagnostics.Debug.WriteLine($"无法应用皮肤 {skinName}: {ex.Message}");
                    // 不抛出异常，允许保存设置
                }
            }
            catch (Exception ex)
            {
                throw new Exception($"保存皮肤设置失败: {ex.Message}");
            }
        }

        public static string GetCurrentSkin()
        {
            return UserLookAndFeel.Default.ActiveSkinName;
        }

        private class SkinConfig
        {
            public string SkinName { get; set; } = "Office 2019 Colorful";
        }
    }
}

